package com.example.demo.controller;

import com.example.demo.pojo.Catrgoryicon;
import com.example.demo.pojo.Goodsaddguanli;
import com.example.demo.service.BookService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author yanglup
 * @create 2020-12-05 21:18
 */
@Controller
public class BackBookController {
    @Autowired
    BookService bookService;

    /***/
    @RequestMapping("/Booklist")
    public String Booklist(Model model, HttpServletRequest request) {
        Integer pageNo=request.getParameter("pageNo")==null?1:Integer.valueOf(request.getParameter("pageNo"));
        PageInfo<Goodsaddguanli> pageInfo=bookService.Booklist(pageNo,8);
        List<Goodsaddguanli> list=pageInfo.getList();
        model.addAttribute("mybooklists",list);
        model.addAttribute("pages",pageInfo.getPages());
        model.addAttribute("pageNo",pageInfo.getPageNum());
        model.addAttribute("pageSize",pageInfo.getPageSize());
        Integer allnumber=bookService.Booklistnumber();
        model.addAttribute("allnumber",allnumber);
        return "Booklist";
    }

//    /**查询全部图书*/
//    @RequestMapping("/Booklist")
//    public String Booklist(Model model){
//        List<Goodsaddguanli> list=bookService.Booklist();
//        model.addAttribute("mybooklists",list);
//        Integer allnumber=bookService.Booklistnumber();
//        model.addAttribute("allnumber",allnumber);
//        return "Booklist";
//    }


    /**图书添加1--跳转到add*/
    @RequestMapping("/Bookadd")
    public String Ordersadd(Model model){
        return "Bookadd";
    }
    /**图书提交2--保存数据跳回list*/
    @PostMapping("/Bookaddtijiao")//post
    public String Bookaddtijiao(Model model, Goodsaddguanli goodsaddguanli){
        //提交到数据库
        Integer cat_id=goodsaddguanli.getCat_id();
        String goods_name=goodsaddguanli.getGoods_name();
        Integer goods_price=goodsaddguanli.getGoods_price();
        Integer goods_number=goodsaddguanli.getGoods_number();
        String school=goodsaddguanli.getSchool();
        String goods_oldnew=goodsaddguanli.getGoods_oldnew();
        String goods_described=goodsaddguanli.getGoods_described();
        String goods_small_logo=goodsaddguanli.getGoods_small_logo();
        String goods_small_logo2=goodsaddguanli.getGoods_small_logo2();
        String goods_small_logo3=goodsaddguanli.getGoods_small_logo3();
        Integer cat_one_id=goodsaddguanli.getCat_one_id();
        Integer cat_two_id=goodsaddguanli.getCat_two_id();
        String goods_serial=goodsaddguanli.getGoods_serial();
        String openid=goodsaddguanli.getOpenid();
        Integer status=goodsaddguanli.getStatus();
        bookService.Bookaddtijiao(cat_id,goods_name,goods_price,goods_number,school,goods_oldnew,goods_described,
                goods_small_logo,goods_small_logo2,goods_small_logo3,cat_one_id,cat_two_id,goods_serial,openid,status);
        return "redirect:/Booklist";
    }


    /**跳转到修改页面*/
    @RequestMapping("/Bookupdate")
    public String Bookupdate(Model model, Integer goods_id, HttpSession session){
        session.setAttribute("goods_id",goods_id);
        Goodsaddguanli goodsaddupdate=bookService.Bookupdate(goods_id);
        model.addAttribute("goodsaddupdate",goodsaddupdate);
        return "Bookupdate";
    }
    /**订单提交2--保存数据跳回list------唯一没有修改goods_id 具体要看商品添加表的goods_id   这里不建议修改*/
    @PostMapping("/Bookupdatetijiao")//post
    public String Bookupdatetijiao(Model model, Goodsaddguanli goodsaddguanli, HttpSession session){
        Integer goods_idchuanru= (Integer) session.getAttribute("goods_id");
        //提交到数据库
        Integer goods_id=goodsaddguanli.getGoods_id();
        Integer cat_id=goodsaddguanli.getCat_id();
        String goods_name=goodsaddguanli.getGoods_name();
        Integer goods_price=goodsaddguanli.getGoods_price();
        Integer goods_number=goodsaddguanli.getGoods_number();
        String school=goodsaddguanli.getSchool();
        String goods_oldnew=goodsaddguanli.getGoods_oldnew();
        String goods_described=goodsaddguanli.getGoods_described();
        String goods_small_logo=goodsaddguanli.getGoods_small_logo();
        String goods_small_logo2=goodsaddguanli.getGoods_small_logo2();
        String goods_small_logo3=goodsaddguanli.getGoods_small_logo3();
        Integer cat_one_id=goodsaddguanli.getCat_one_id();
        Integer cat_two_id=goodsaddguanli.getCat_two_id();
        String openid=goodsaddguanli.getOpenid();
        Integer status=goodsaddguanli.getStatus();
        bookService.Bookupdatetijiao(goods_idchuanru,goods_id,cat_id,goods_name,goods_price,goods_number,school,goods_oldnew,goods_described,
                goods_small_logo,goods_small_logo2,goods_small_logo3,cat_one_id,cat_two_id,openid,status);
        return "redirect:/Booklist";
    }



    /**删除商品*/
    @RequestMapping("/Bookdelete")
    public String Bookdelete(Integer goods_id){
        bookService.Bookdelete(goods_id);
        return "redirect:/Booklist";
    }

    /**通过id搜索商品*/
    @RequestMapping("/Booksearch")
    public String Booksearch(String goods_name, Model model){
        List<Goodsaddguanli> booksearchlist=bookService.Booksearch(goods_name);
        model.addAttribute("booksearchlists",booksearchlist);
        Integer allnumber=bookService.Booksearchnumber(goods_name);
        model.addAttribute("allnumber",allnumber);
        return "Booklistquery";
    }

}
